package com.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.model.Chengji;
import com.model.Document;
import com.service.DocumentService;
import com.utils.ExportExcel;
import com.utils.Util;
import com.vto.ScoreVTO;

@Controller
public class DocController {
	//文件上传到的目录
	private static final String filePath = "/Users/abel/tmp/docupload/";
	@Autowired
	private DocumentService documentService;
	@RequestMapping(value = "/docList")
	public String docList(HttpServletRequest request) {
		List<Document> list = documentService.queryList();
		request.setAttribute("docList", list);
		return "/admin/docmanage/list.jsp";
	}
	@RequestMapping(value = "/toUpload")
	public String toUpload(HttpServletRequest request)  {
		List<Document> list = documentService.queryList();
		request.setAttribute("docList", list);
		return "/admin/docmanage/upload.jsp";
	}
	@RequestMapping(value = "/upload")
	public String docUpload(@RequestParam("fujian") MultipartFile file,HttpServletRequest request){
		String fileOriginalName = file.getOriginalFilename();
		String name = Util.getUUID() + fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1);
		String finalName = filePath + name;
		Util.transferDoc(file, name, filePath);
		Document document = new Document();
		document.setLocation(finalName);
		document.setName(fileOriginalName);
		documentService.insert(document);
		List<Document> list = documentService.queryList();
		request.setAttribute("docList", list);
		return "/admin/docmanage/list.jsp";
	}
	@RequestMapping(value = "/download")
	public HttpServletResponse download(@RequestParam("id")Integer id,HttpServletRequest request, HttpServletResponse response){
		Document doc = documentService.queryById(id);
		String path = doc.getLocation();
		 try {
	            // path是指欲下载的文件的路径。
	            File file = new File(path);
	            // 取得文件名。
	            String filename = file.getName();
	            // 以流的形式下载文件。
	            InputStream fis = new BufferedInputStream(new FileInputStream(path));
	            byte[] buffer = new byte[fis.available()];
	            fis.read(buffer);
	            fis.close();
	            // 清空response
	            response.reset();
	            // 设置response的Header
	            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
	            response.addHeader("Content-Length", "" + file.length());
	            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	            response.setContentType("application/octet-stream");
	            toClient.write(buffer);
	            toClient.flush();
	            toClient.close();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
		return response;
	}
	
	@RequestMapping(value = "/exportScore")
	public void exportScore(@RequestParam("cid")Integer cid,HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<ScoreVTO> list= documentService.queryScoreList(cid);
		String[] rowsName = {"序号","课程","班级","学生姓名","分数"};
		List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < list.size(); i++) {
        	ScoreVTO vto = list.get(i);
            objs = new Object[rowsName.length];
            objs[1] = vto.getCourseName();
            objs[2] = vto.getClassName();
            objs[3] = vto.getStudentName();
            objs[4] = vto.getScores();
            dataList.add(objs);
        }
        new ExportExcel("学生分数表", rowsName, dataList).export(response);
	}
}
